﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Web;
using System.Web.Http;
using System.Web.Http.Cors;
using XQ.Framework.io.text;
using XQ.WeChatOfficialAccount.BLL.Sys;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.Model.Sys;
using XQ.WeChatOfficialAccount.WebAPI.Filter;

namespace XQ.WeChatOfficialAccount.WebAPI.Controllers.System
{
    [FormAuthenticationFilter]
    public class ButtonMenuController : ApiController
    {
        private readonly ButtonMenuBll _buttonMenu = new ButtonMenuBll();
        private readonly RightMangerBll _rightMangerBll = new RightMangerBll();
        
        /// <summary>
        /// 获取所有按钮
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/ButtonMenu/GetButtonses")]
        public IEnumerable<Sys_Buttons> GetButtonses()
        {
            return _buttonMenu.GetButtonses();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="buttonString"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Api/ButtonMenu/SaveButtonin")]
        public string SaveButtonin(string buttonString)
        {
            var button = JsonHelper.ParseJson<Sys_Buttons>(buttonString);
            return _buttonMenu.SaveButton(button);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Api/ButtonMenu/DeleteButton")]
        public bool DeleteButton(int id)
        {
            return _buttonMenu.DeleteButton(id);
        }

        /// <summary>
        /// 获取一个按钮
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/ButtonMenu/GetButton")]
        public Sys_Buttons GetButton(int id)
        {
            return _buttonMenu.GetButton(id);
        }

        /// <summary>
        /// 获取页面按钮
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/ButtonMenu/GetMenuButtonses")]
        public IEnumerable<CheckInfo> GetMenuButtonses(string ids)
        {
            var data = new List<CheckInfo>();
            if (!string.IsNullOrEmpty(ids))
            {
                string[] _ids = ids.Split('%');

                for (int i = 0; i < _ids.Length; i++)
                {
                    var item = new CheckInfo();
                    item.ControlId = _ids[i].Split('$')[0];
                    string menuIdAndRoleIdAndBtnIds = _ids[i].Split('$')[1];
                    item.Result =
                        _buttonMenu.GetMenuButtonsesInfo(Convert.ToInt32(menuIdAndRoleIdAndBtnIds.Split(',')[0]),
                            Convert.ToInt32(menuIdAndRoleIdAndBtnIds.Split(',')[1]));
                    data.Add(item);
                }
            }

            return data;
        }

        /// <summary>
        /// 保存页面按钮
        /// </summary>
        /// <param name="menuId">菜单ID</param>
        /// <param name="btnId">菜单ID</param>
        /// <param name="flag">标记</param>
        [HttpPost]
        [Route("Api/ButtonMenu/SaveMenuButtons")]
        public bool SaveMenuButtons(int menuId, int btnId, bool flag)
        {
            _buttonMenu.SaveMenuButtons(menuId, btnId, flag);

            return true;
        }

        /// <summary>
        /// 根据角色取出所有菜单及按钮
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/ButtonMenu/GetMenuButtonsByRoleId")]
        public IEnumerable<MenuButtonModel> GetMenuButtonsByRoleId()
        {
            //获取用户信息
            IPrincipal principal = HttpContext.Current.User;
            int userId = int.Parse(principal.Identity.Name);
            int roleId = _rightMangerBll.GetRolesByUserId(userId).ID;

            return _buttonMenu.GetMenuButtons(roleId);
        }
    }
}